package org.zpmis.api.http;

import org.springframework.validation.annotation.Validated;
import org.zpmis.api.business.PurchaseWebService;
import org.zpmis.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.project.qo.AddProjectQo;
import org.zpmis.domain.purchase.qo.AddPurchaseQo;
import org.zpmis.domain.purchase.qo.DelPurchaseQo;
import org.zpmis.domain.purchase.qo.EditPurchaseQo;
import org.zpmis.domain.purchase.qo.PurchaseListQo;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 采购单控制类
* @author  Yang
* date: 2021-6-2
* time: 14:54:17
*/
@RestController
@RequestMapping("/purchase")
@Slf4j
public class PurchaseController extends BaseController{

    @Resource
    private PurchaseWebService purchaseWebService;


    /**
     * 新增采购单
     * @param addPurchaseQo
     * @return
     */
    @PostMapping("/addPurchase")
    public PlatformResult addPurchase(@Validated @RequestBody AddPurchaseQo addPurchaseQo){
        try {
            purchaseWebService.addPurchase(addPurchaseQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[PurchaseController]---addPurchase--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[PurchaseController]---addPurchase--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 修改采购单
     * @param editPurchaseQo
     * @return
     */
    @PostMapping("/editPurchase")
    public PlatformResult editPurchase(@Validated @RequestBody EditPurchaseQo editPurchaseQo){
        try {
            purchaseWebService.editPurchase(editPurchaseQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[PurchaseController]---editPurchase--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[PurchaseController]---editPurchase--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 删除采购单
     * @param delPurchaseQo
     * @return
     */
    @PostMapping("/delPurchase")
    public PlatformResult delPurchase(@Validated @RequestBody DelPurchaseQo delPurchaseQo){
        try {
            purchaseWebService.delPurchase(delPurchaseQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[PurchaseController]---delPurchase--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[PurchaseController]---delPurchase--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 采购单列表查询
     * @param purchaseListQo
     * @return
     */
    @PostMapping("/purchaseList")
    public PlatformResult purchaseList(@Validated @RequestBody PurchaseListQo purchaseListQo){
        try {
            return PlatformResult.success(purchaseWebService.purchaseList(purchaseListQo));
        } catch (BusinessException e) {
            log.error("[PurchaseController]---purchaseList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[PurchaseController]---purchaseList--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
